package com.server.Service;

import com.server.Example.SessionsExample;
import com.server.entity.Movies;
import com.server.entity.MoviesToType;
import com.server.entity.Sessions;
import com.server.mapper.MoviesMapper;
import com.server.mapper.SessionsMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;
import java.util.Random;

/**
 * The type Sessions service.
 *
 * @author user
 */
public class SessionsService {

    /**
     * Select vales Movie show information.
     * Inquire about other movie screenings in the future
     *
     * @return String JsonList Calling method: SessionsService.nextWeekMovies()
     */
    public static ArrayList<Sessions> nextWeekMovies(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        SessionsMapper sessionsMapper = sqlSession.getMapper(SessionsMapper.class);
        SessionsExample sessionsExample = new SessionsExample();
        Date date = new Date();
        sessionsExample.createCriteria().andSessionsStartTimeNotBetween(date,new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000L));
        ArrayList<Sessions> moviesList = (ArrayList<Sessions>)sessionsMapper.selectByExample(sessionsExample);
        sqlSession.close();
        return moviesList;
    }

    /**
     * Select by primary key movies show information.
     * Get information about a single piece of data
     *
     * @param id the id
     * @return the Session Calling method: SessionsService.selectByPrimaryKey(id)
     */
    public static Movies selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesMapper movies = sqlSession.getMapper(MoviesMapper.class);
        Movies movie=movies.selectByPrimaryKey(id);
        sqlSession.close();
        return movie == null ?new Movies():movie;
    }


    /**
     * Instr vales int.
     * Generate the relevant data of the movie schedule
     *
     * @param number    the number
     * @param startTime the start time
     * @param entire    the entire
     * @return the int Calling method: SessionsService.instrVales(number，startTime，entire )
     */
    public static int instrVales(int number, String startTime, String entire){
        int sumCount = 0;
        SqlSession sqlSession=MybatisClass.getSqlSession();
        SessionsMapper sessionsMapper = sqlSession.getMapper(SessionsMapper.class);
        ArrayList<MoviesToType> moviesToTypes=MoviesToTypeService.selectMovieToType();
        for (int i = 0; i < number; i++) {
            Date date = Utils.randomDate(startTime, entire);
            sumCount = sumCount + sessionsMapper.insert(new Sessions(
                    moviesToTypes.get(new Random().nextInt(moviesToTypes.size())).getMoviesToTypeId(),
                    date,
                    new Date(Objects.requireNonNull(date).getTime()+90*1000*60),
                    new Date(date.getTime()-240*1000*60)
                    ));
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }
}
